package com.qinghe.utils;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * 
 * 项目名称：feing
 * 类名称：ImageCut
 * 类描述：图片剪切工具类
 * 创建人：longfei
 * 创建时间：2014-3-25 下午8:18:29
 * @version 
 * 
 */
public class ImageCut {

	/**
	 * 剪切图片
	 * 
	 * @param srcImageFile 源图像地址
	 * @param dirImageFile 新图像地址
	 * @param x 目标切片起点x坐标
	 * @param y 目标切片起点y坐标
	 * @param destWidth 目标切片宽度
	 * @param destHeight 目标切片高度
	 * @throws IOException
	 */
	public static void cut(String srcImageFile, String dirImageFile, int x,
			int y, int destWidth, int destHeight) throws IOException {
		FileInputStream is = null;
		ImageInputStream iis = null;
		try {
			is = new FileInputStream(srcImageFile);
			Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");
			ImageReader reader = it.next();
			iis = ImageIO.createImageInputStream(is);
			reader.setInput(iis, true);
			ImageReadParam param = reader.getDefaultReadParam();
			Rectangle rect = new Rectangle(x, y, destWidth, destHeight);
			param.setSourceRegion(rect);
			BufferedImage bi = reader.read(0, param);
			// 目录不存在的情况下，创建目录
			File file = new File(dirImageFile);
			if (!file.exists()) {
                file.mkdirs();
			}
			ImageIO.write(bi, "jpg", file);
		} finally {
			if (is != null)
				is.close();
			if (iis != null)
				iis.close();
		}
	}
	
	/**
	 * 图像切割
	 * 
	 * @param srcImageFile 源图像地址
	 * @param dirImageFile 新图像地址
	 * @param x 目标切片起点x坐标
	 * @param y 目标切片起点y坐标
	 * @param destWidth 目标切片宽度
	 * @param destHeight 目标切片高度
	 */
	public static void abscut(String srcImageFile, String dirImageFile, int x,
			int y, int destWidth, int destHeight) {
		try {
			Image img;
			ImageFilter cropFilter;
			// 读取源图像
			BufferedImage bi = ImageIO.read(new File(srcImageFile));
			int srcWidth = bi.getWidth();// 源图宽度
			int srcHeight = bi.getHeight();// 源图高度
			if (srcWidth >= destWidth && srcHeight >= destHeight) {
				Image image = bi.getScaledInstance(srcWidth, srcHeight,
						Image.SCALE_DEFAULT);
				// 改进的想法:是否可用多线程加快切割速度
				// 四个参数分别为图像起点坐标和宽高
				// 即: CropImageFilter(int x,int y,int width,int height)
				cropFilter = new CropImageFilter(x, y, destWidth, destHeight);
				img = Toolkit.getDefaultToolkit().createImage(
						new FilteredImageSource(image.getSource(), cropFilter));
				BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
				Graphics g = tag.getGraphics();
				g.drawImage(img, 0, 0, null);// 绘制缩小后的图
				g.dispose();
				// 目录不存在的情况下，创建目录
				File file = new File(dirImageFile);
				if (!file.exists()) {
	                file.mkdirs();
				}
				// 输出为文件
				ImageIO.write(tag, "JPEG", file);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 缩放图像
	 * 
	 * @param srcImageFile 源图像文件地址
	 * @param result 缩放后的图像地址
	 * @param scale 缩放比例
	 * @param flag 缩放选择:true 放大; false 缩小;
	 */
	public static void scale(String srcImageFile, String result, int scale,
			boolean flag) {
		try {
			BufferedImage src = ImageIO.read(new File(srcImageFile));// 读入文件
			int width = src.getWidth();// 得到源图宽
			int height = src.getHeight();// 得到源图高
			if (flag) {
				// 放大
				width = width * scale;
				height = height * scale;
			} else {
				// 缩小
				width = width / scale;
				height = height / scale;
			}
			Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
			BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null);// 绘制缩小后的图
			g.dispose();
			// 目录不存在的情况下，创建目录
			File file = new File(result);
			if (!file.exists()) {
                file.mkdirs();
			}
			ImageIO.write(tag, "JPEG", file);// 输出到文件流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 按比例缩放图像
	 * 
	 * @param srcImageFile 源图像文件地址
	 * @param imgW 定义允许图片宽度
	 * @param imgH 定义允许图片高度
	 */
	public static void resizeImg(String srcImageFile, int imgW, int imgH) {
		int w = imgW;
		int h = imgH;
		try {
			BufferedImage src = ImageIO.read(new File(srcImageFile));// 读入文件
			int width = src.getWidth();// 得到源图宽
			int height = src.getHeight();// 得到源图高
			if (width > 0 && height > 0) {
				if (width / height >= imgW / imgH) {
					if (width > imgW) {
						w = imgW;
						h = (height * imgW) / width;
					} else {
						w = width;
						h = height;
					}
				} else {
					if (height > imgH) {
						h = imgH;
						w = (width * imgH) / height;
					} else {
						w = width;
						h = height;
					}
				}
			}
			ImageCut.scale(srcImageFile, srcImageFile, w, h);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 重新生成按指定宽度和高度的图像
	 * 
	 * @param srcImageFile 源图像文件地址
	 * @param result 新的图像地址
	 * @param _width 设置新的图像宽度
	 * @param _height 设置新的图像高度
	 */
	public static void scale(String srcImageFile, String result, int _width, int _height) {
		scale(srcImageFile, result, _width, _height, 0, 0);
	}

	public static void scale(String srcImageFile, String result, int _width,
			int _height, int x, int y) {
		try {
			BufferedImage src = ImageIO.read(new File(srcImageFile)); // 读入文件

			int width = src.getWidth();// 得到源图宽
			int height = src.getHeight();// 得到源图高

			if (width > _width) {
				width = _width;
			}
			if (height > _height) {
				height = _height;
			}
			Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
			BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, x, y, null);// 绘制缩小后的图
			g.dispose();
			// 目录不存在的情况下，创建目录
			File file = new File(result);
			if (!file.exists()) {
                file.mkdirs();
			}
			ImageIO.write(tag, "JPEG", file);// 输出到文件流
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 图像类型转换 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
	 */
	public static void convert(String source, String result) {
		try {
			File f = new File(source);
			f.canRead();
			f.canWrite();
			BufferedImage src = ImageIO.read(f);
			// 目录不存在的情况下，创建目录
			File file = new File(result);
			if (!file.exists()) {
                file.mkdirs();
			}
			ImageIO.write(src, "JPG", file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 彩色转为黑白
	 * 
	 * @param source
	 * @param result
	 */
	public static void gray(String source, String result) {
		try {
			BufferedImage src = ImageIO.read(new File(source));
			ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
			ColorConvertOp op = new ColorConvertOp(cs, null);
			src = op.filter(src, null);
			// 目录不存在的情况下，创建目录
			File file = new File(result);
			if (!file.exists()) {
                file.mkdirs();
			}
			ImageIO.write(src, "JPEG", file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
